package org.example.wumeng.controller;

import org.example.wumeng.pojo.*;
import org.example.wumeng.service.T_doctorSpecialtyService;
import org.example.wumeng.service.T_bookCategoryService;
import org.example.wumeng.service.T_prescriptionCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分类管理控制器
 */
@RestController
@RequestMapping("/categories")
@CrossOrigin
public class T_categoryController {

    @Autowired
    private T_doctorSpecialtyService doctorSpecialtyService;

    @Autowired
    private T_bookCategoryService bookCategoryService;

    @Autowired
    private T_prescriptionCategoryService prescriptionCategoryService;

    // ==================== 医家专长分类 ====================

    @GetMapping("/doctor-specialties")
    public Result<List<DoctorSpecialty>> getDoctorSpecialties() {
        List<DoctorSpecialty> list = doctorSpecialtyService.getActiveSpecialties();
        return Result.success(list);
    }

    @PostMapping("/doctor-specialties")
    public Result<Long> createDoctorSpecialty(@RequestBody DoctorSpecialty specialty) {
        Long id = doctorSpecialtyService.createSpecialty(specialty);
        return Result.success(id);
    }

    @PutMapping("/doctor-specialties/{id}")
    public Result<Void> updateDoctorSpecialty(@PathVariable Long id, @RequestBody DoctorSpecialty specialty) {
        doctorSpecialtyService.updateSpecialty(id, specialty);
        return Result.success(null);
    }

    @DeleteMapping("/doctor-specialties/{id}")
    public Result<Void> deleteDoctorSpecialty(@PathVariable Long id) {
        doctorSpecialtyService.deleteSpecialty(id);
        return Result.success(null);
    }

    @GetMapping("/doctor-specialties/statistics")
    public Result<List<CategoryStatistics>> getDoctorSpecialtyStatistics() {
        List<Map<String, Object>> statistics = doctorSpecialtyService.getSpecialtyStatistics();
        List<CategoryStatistics> result = statistics.stream()
                .map(stat -> new CategoryStatistics(
                        (String) stat.get("name"),
                        ((Number) stat.get("count")).longValue()
                ))
                .collect(Collectors.toList());
        return Result.success(result);
    }
    @GetMapping("/doctor-specialties/{specialtyName}/doctors")
    public Result<List<Map<String, Object>>> getDoctorsBySpecialty(@PathVariable String specialtyName) {
        List<Map<String, Object>> doctors = doctorSpecialtyService.getDoctorsBySpecialty(specialtyName);
        return Result.success(doctors);
    }
    // ==================== 医书分类 ====================

    @GetMapping("/book-categories")
    public Result<List<BookCategory>> getBookCategories() {
        List<BookCategory> list = bookCategoryService.getActiveCategories();
        return Result.success(list);
    }

    @PostMapping("/book-categories")
    public Result<Long> createBookCategory(@RequestBody BookCategory category) {
        Long id = bookCategoryService.createCategory(category);
        return Result.success(id);
    }

    @PutMapping("/book-categories/{id}")
    public Result<Void> updateBookCategory(@PathVariable Long id, @RequestBody BookCategory category) {
        bookCategoryService.updateCategory(id, category);
        return Result.success(null);
    }

    @DeleteMapping("/book-categories/{id}")
    public Result<Void> deleteBookCategory(@PathVariable Long id) {
        bookCategoryService.deleteCategory(id);
        return Result.success(null);
    }

    @GetMapping("/book-categories/statistics")
    public Result<List<CategoryStatistics>> getBookCategoryStatistics() {
        List<Map<String, Object>> statistics = bookCategoryService.getCategoryStatistics();
        List<CategoryStatistics> result = statistics.stream()
                .map(stat -> new CategoryStatistics(
                        (String) stat.get("name"),
                        ((Number) stat.get("count")).longValue()
                ))
                .collect(Collectors.toList());
        return Result.success(result);
    }
    @GetMapping("/book-categories/{categoryName}/books")
    public Result<List<Map<String, Object>>> getBooksByCategory(@PathVariable String categoryName) {
        List<Map<String, Object>> books = bookCategoryService.getBooksByCategory(categoryName);
        return Result.success(books);
    }
    // ==================== 方剂分类 ====================

    @GetMapping("/prescription-categories")
    public Result<List<PrescriptionCategory>> getPrescriptionCategories() {
        List<PrescriptionCategory> list = prescriptionCategoryService.getActiveCategories();
        return Result.success(list);
    }

    @PostMapping("/prescription-categories")
    public Result<Long> createPrescriptionCategory(@RequestBody PrescriptionCategory category) {
        Long id = prescriptionCategoryService.createCategory(category);
        return Result.success(id);
    }

    @PutMapping("/prescription-categories/{id}")
    public Result<Void> updatePrescriptionCategory(@PathVariable Long id, @RequestBody PrescriptionCategory category) {
        prescriptionCategoryService.updateCategory(id, category);
        return Result.success(null);
    }

    @DeleteMapping("/prescription-categories/{id}")
    public Result<Void> deletePrescriptionCategory(@PathVariable Long id) {
        prescriptionCategoryService.deleteCategory(id);
        return Result.success(null);
    }

    @GetMapping("/prescription-categories/statistics")
    public Result<List<CategoryStatistics>> getPrescriptionCategoryStatistics() {
        List<Map<String, Object>> statistics = prescriptionCategoryService.getCategoryStatistics();
        List<CategoryStatistics> result = statistics.stream()
                .map(stat -> new CategoryStatistics(
                        (String) stat.get("name"),
                        ((Number) stat.get("count")).longValue()
                ))
                .collect(Collectors.toList());
        return Result.success(result);
    }
    @GetMapping("/prescription-categories/{categoryName}/prescriptions")
    public Result<List<Map<String, Object>>> getPrescriptionsByCategory(@PathVariable String categoryName) {
        List<Map<String, Object>> prescriptions = prescriptionCategoryService.getPrescriptionsByCategory(categoryName);
        return Result.success(prescriptions);
    }
}
